using System;
using System.Collections ;
using System.Text ;
using System.Data ;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.MF.DataEntity;
using WP.MF.DataAccess;
using WP.IM.DataAccess;

namespace WP.MF.BusinessRule
{
	public class MLocationBRL : RuleBase
	{
		public MLocationBRL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region CommonValid
        private bool CommonValid(DBAccess dBAccess, DataRow dr, ref string strErrMsg)
        {
    
            strErrMsg = "";

            return true;
        }
        #endregion

        public bool FieldCheck(DBAccess dbcon, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {

            bool bValid = true;
            string strValue = "";
            string strAcctNo = "", strProfitNo = "", strBranchno = "";

            strErrPrompt = "";

            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;

            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);
            string strInterCompanyAcctno = dr[MLocationDEL.INTERCOMPANYACCTNO].ToString();
            string strInterCompanyProfitno = dr[MLocationDEL.INTERCOMPANYPROFITNO].ToString();
            string strInterCompanyBranchno = dr[MLocationDEL.INTERCOMPANYBRANCHNO].ToString();

            switch (strFieldName.ToUpper().Trim())
            {
                case "LOCATIONNO":
                    if (formStatus == FormStatus.AddNew)
                        return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MLocationDEL.TABLENAME_MLOCATION, strFieldName, ref strErrPrompt);
                    else
                        return true;

                #region check ProfitNo
                case "PROFITNO":
                    {
                        strValue = dr[MLocationDEL.PROFITNO].ToString();

                        if (DataFunctions.GetmSwitchValue(dbcon, "GLInventoryPCbyLoc", "IM"))
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "glprofitcenter", strValue, "Profitno");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_ProfitNoNotOnFile");
                                return bValid;
                            }
                        }
                        else
                        {
                            if (strValue.Trim() != "")
                            {
                                bValid = DataFunctions.CheckRecordExist(dbcon, "glprofitcenter", strValue, "Profitno");
                                if (!bValid)
                                {
                                    strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_ProfitNoNotOnFile");
                                    return bValid;
                                }
                            }
                        }
                        return true;
                    }
                #endregion

                #region check DirectOnly
                case "DIRECTONLY":
                    {
                        strValue = dr[MLocationDEL.DIRECTONLY].ToString();
                        if (BaseFunctions.GetBoolValue(strValue))
                        {
                            if (BaseFunctions.GetBoolValue(dr[MLocationDEL.REMANLOCATION].ToString()))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_DirectLocCanNotBeRemanLoc");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check TaGPreFix
                case "TAGPREFIX":
                    {
                        strValue = dr[MLocationDEL.TAGPREFIX].ToString();
                        if (strValue.Trim() != "")
                        {
                            if (BaseFunctions.IsNumeric(strValue))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_TagPrefixCanNotBeNummeric");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check RemanLocation
                case "REMANLOCATION":
                    {
                        strValue = dr[MLocationDEL.REMANLOCATION].ToString();
                        if (BaseFunctions.GetBoolValue(strValue))
                        {
                            if (BaseFunctions.GetBoolValue(dr[MLocationDEL.DIRECTONLY].ToString()))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_RemanLocCanNotBeDirectLoc");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check IsShareLoc
                case "ISSHARELOC":
                    {
                        strValue = dr[MLocationDEL.ISSHARELOC].ToString();
                        if (BaseFunctions.GetBoolValue(strValue))
                        {
                            if (BaseFunctions.GetBoolValue(dr[MLocationDEL.DIRECTONLY].ToString()))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_SharedLocMustBeInventoryLoc");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check MainCashier
                case "MAINCASHIER":
                    {
                        return true;
                    }
                #endregion

                #region check GoodsRecvSurAcctNo,GoodsRecvSurProfitNo,GoodsRecvSurBranchNo
                case "GOODSRECVSURACCTNO":
                    {
                        strAcctNo = dr[MLocationDEL.GOODSRECVSURACCTNO].ToString();
                        strProfitNo = dr[MLocationDEL.GOODSRECVSURPROFITNO].ToString();
                        strBranchno = dr[MLocationDEL.GOODSRECVSURBRANCHNO].ToString();

                        if (strAcctNo.Trim() != "" || strProfitNo.Trim() != "" || strBranchno.Trim() != "" || Convert.ToDouble(BaseFunctions.FormatNumeric(dr[MLocationDEL.GOODSRECVSURPCT].ToString(), "n.n")) > 0)
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "glAccount", strAcctNo, "AcctNo", "ProfitNo='" + strProfitNo + "' and BranchNo='" + strBranchno + "' and (AcctType='A' or AcctType='C')");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_SurchargeGLNoNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                case "GOODSRECVSURPROFITNO":
                    {
                        strAcctNo = dr[MLocationDEL.GOODSRECVSURACCTNO].ToString();
                        strProfitNo = dr[MLocationDEL.GOODSRECVSURPROFITNO].ToString();
                        strBranchno = dr[MLocationDEL.GOODSRECVSURBRANCHNO].ToString();

                        if (strAcctNo.Trim() != "" || strProfitNo.Trim() != "" || strBranchno.Trim() != "" || Convert.ToDouble(BaseFunctions.FormatNumeric(dr[MLocationDEL.GOODSRECVSURPCT].ToString(), "n.n")) > 0)
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "glAccount", strAcctNo, "AcctNo", "ProfitNo='" + strProfitNo + "' and BranchNo='" + strBranchno + "' and (AcctType='A' or AcctType='C')");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_SurchargeGLNoNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                case "GOODSRECVSURBRANCHNO":
                    {
                        strAcctNo = dr[MLocationDEL.GOODSRECVSURACCTNO].ToString();
                        strProfitNo = dr[MLocationDEL.GOODSRECVSURPROFITNO].ToString();
                        strBranchno = dr[MLocationDEL.GOODSRECVSURBRANCHNO].ToString();

                        if (strAcctNo.Trim() != "" || strProfitNo.Trim() != "" || strBranchno.Trim() != "" || Convert.ToDouble(BaseFunctions.FormatNumeric(dr[MLocationDEL.GOODSRECVSURPCT].ToString(), "n.n")) > 0)
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "glAccount", strAcctNo, "AcctNo", "ProfitNo='" + strProfitNo + "' and BranchNo='" + strBranchno + "' and (AcctType='A' or AcctType='C')");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_SurchargeGLNoNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check CommAcctNo,CommProfitNo,CommBranchNo
                case "COMMACCTNO":
                case "COMMPROFITNO":
                case "COMMBRANCHNO":
                    strAcctNo = dr[MLocationDEL.COMMACCTNO].ToString();
                    strProfitNo = dr[MLocationDEL.COMMPROFITNO].ToString();
                    strBranchno = dr[MLocationDEL.COMMBRANCHNO].ToString();

                    if (strAcctNo.Trim() != "" || strProfitNo.Trim() != "" || strBranchno.Trim() != "")
                    {
                        bValid = DataFunctions.CheckRecordExist(dbcon, "glAccount", strAcctNo, "AcctNo", "ProfitNo='" + strProfitNo + "' and BranchNo='" + strBranchno + "' and (AcctType='A' or AcctType='C')");
                        if (!bValid)
                        {
                            strErrPrompt = DataFunctions.GetMsg(dbcon, "AccountNotExist");
                            return bValid;
                        }
                    }
                    return true;

                #endregion

                #region check PLAcctNo,PLProfitNo,PLBranchNo
                case "PLACCTNO":
                    {
                        strAcctNo = dr[MLocationDEL.PLACCTNO].ToString();
                        strProfitNo = dr[MLocationDEL.PLPROFITNO].ToString();
                        strBranchno = dr[MLocationDEL.PLBRANCHNO].ToString();

                        if (strAcctNo.Trim() != "" || strProfitNo.Trim() != "" || strBranchno.Trim() != "")
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "glAccount", strAcctNo, "AcctNo", "ProfitNo='" + strProfitNo + "' and BranchNo='" + strBranchno + "' and (AcctType='A' or AcctType='C')");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_RemanProfitGLNoNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                case "PLPROFITNO":
                    {
                        strAcctNo = dr[MLocationDEL.PLACCTNO].ToString();
                        strProfitNo = dr[MLocationDEL.PLPROFITNO].ToString();
                        strBranchno = dr[MLocationDEL.PLBRANCHNO].ToString();

                        if (strAcctNo.Trim() != "" || strProfitNo.Trim() != "" || strBranchno.Trim() != "")
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "glAccount", strAcctNo, "AcctNo", "ProfitNo='" + strProfitNo + "' and BranchNo='" + strBranchno + "' and (AcctType='A' or AcctType='C')");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_RemanProfitGLNoNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                case "PLBRANCHNO":
                    {
                        strAcctNo = dr[MLocationDEL.PLACCTNO].ToString();
                        strProfitNo = dr[MLocationDEL.PLPROFITNO].ToString();
                        strBranchno = dr[MLocationDEL.PLBRANCHNO].ToString();

                        if (strAcctNo.Trim() != "" || strProfitNo.Trim() != "" || strBranchno.Trim() != "")
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "glAccount", strAcctNo, "AcctNo", "ProfitNo='" + strProfitNo + "' and BranchNo='" + strBranchno + "' and (AcctType='A' or AcctType='C')");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_RemanProfitGLNoNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check InterCompanyAcctno,InterCompanyProfitno,InterCompanyBranchno
                case "INTERCOMPANYACCTNO":
                case "INTERCOMPANYPROFITNO":
                case "INTERCOMPANYBRANCHNO":
                    if (strInterCompanyAcctno.Trim() != string.Empty && strInterCompanyProfitno.Trim() != string.Empty && strInterCompanyBranchno.Trim() != string.Empty)
                    {
                        if (!DataFunctions.CheckNoInActiveAccount(dbcon, strInterCompanyAcctno, strInterCompanyProfitno, strInterCompanyBranchno, true, "C", "", ref strErrPrompt))
                            return false;
                    }
                    return true;

                #endregion

                #region check MasterBuyLocation
                case "MASTERBUYLOCATION":
                    {
                        strValue = dr[MLocationDEL.MASTERBUYLOCATION].ToString();
                        if (strValue.ToUpper().Trim() != dr[MLocationDEL.LOCATIONNO].ToString().ToUpper().Trim())
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "mLocation", strValue, "LocationNo");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_BuyLocNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check TransferLocation
                case "TRANSFERLOCATION":
                    {
                        strValue = dr[MLocationDEL.TRANSFERLOCATION].ToString();
                        if (BaseFunctions.GetBoolValue(strValue))
                        {
                            if (BaseFunctions.GetBoolValue(dr[MLocationDEL.DIRECTONLY].ToString()))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_DirectLocation");
                                return bValid;
                            }
                            if (BaseFunctions.GetBoolValue(dr[MLocationDEL.REMANLOCATION].ToString()))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_RemanLocation");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check VmiCustNo
                case "VMICUSTNO":
                    {
                        strValue = dr[MLocationDEL.VMICUSTNO].ToString();

                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.VMILOCATION].ToString()))
                        {
                            if (dr[MLocationDEL.VMITYPE].ToString().ToUpper().Trim() == "S")
                            {
                                if (strValue.Trim() == "")
                                {
                                    bValid = false;
                                    strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_VMISupNoCanNotBeEmpty");
                                    return bValid;
                                }
                                bValid = DataFunctions.CheckRecordExist(dbcon, "ApSupplier", strValue, "SupNo");
                                if (!bValid)
                                {
                                    strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_VMISupNoNotOnFile");
                                    return bValid;
                                }
                            }
                            else
                            {
                                if (strValue.Trim() == "")
                                {
                                    bValid = false;
                                    strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_VMICustNoCanNotBeEmpty");
                                    return bValid;
                                }
                                bValid = DataFunctions.CheckRecordExist(dbcon, "ArCustomer", strValue, "CustNo");
                                if (!bValid)
                                {
                                    strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_VMICustNoNotOnFile");
                                    return bValid;
                                }
                            }
                        }
                        return true;
                    }
                #endregion

                #region check vmiUser
                case "VMIUSER":
                    {
                        strValue = dr[MLocationDEL.VMIUSER].ToString();
                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.VMILOCATION].ToString()))
                        {
                            if (strValue.Trim() == "")
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_VMIUserCanNotBeEmpty");
                                return bValid;
                            }
                            bValid = DataFunctions.CheckRecordExist(dbcon, "mUser", strValue, "UserName");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_VMIUserNotOnFile");
                                return bValid;
                            }

                        }
                        return true;
                    }
                #endregion

                #region check VmiType
                case "VMITYPE":
                    {
                        strValue = dr[MLocationDEL.VMITYPE].ToString();
                        strValue = strValue.ToUpper().Trim();

                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.VMILOCATION].ToString()))
                        {
                            if (strValue != "S" && strValue != "C" && strValue != "M")
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_VMITypeMustBySOrCOrM");
                                return bValid;
                            }
                            if (strValue == "S" && !BaseFunctions.GetBoolValue(dr[MLocationDEL.DIRECTONLY].ToString()))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_LocMustBeDirectonlyLoc");
                                return bValid;
                            }
                            if ((strValue == "C" || strValue == "M") && BaseFunctions.GetBoolValue(dr[MLocationDEL.DIRECTONLY].ToString()))
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_LocCanNotBeDirectonlyLoc");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check InterCmpyDB
                case "INTERCMPYDB":
                    {
                        strValue = dr[MLocationDEL.INTERCMPYDB].ToString();
                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.ISINTERCOMPANY].ToString()))
                        {
                            if (strValue.Trim() == "")
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_IntercompanyDBName");
                                return bValid;
                            }
                            DBAccess dbconInterComp = null;
                            if (CvtFunc.ToString(dr[MLocationDEL.INTERCMPYDBPATH]).Trim() != "" && !this.CreateInterDBConn(dr[MLocationDEL.INTERCMPYDB].ToString(), dr[MLocationDEL.INTERCMPYDBPATH].ToString(), ref dbconInterComp))
                            {
                                strErrPrompt = Msg.Select("Can not connect intercompany database.", DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_CanNotConnectIntercompanyDB"));
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check InterCmpyDBPath
                case "INTERCMPYDBPATH":
                    {
                        strValue = dr[MLocationDEL.INTERCMPYDBPATH].ToString();
                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.ISINTERCOMPANY].ToString()))
                        {
                            if (strValue.Trim() == "")
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_IntercompanyDBServer");
                                return bValid;
                            }
                            DBAccess dbconInterComp = null;
                            if ( CvtFunc.ToString(dr[MLocationDEL.INTERCMPYDB]).Trim()!="" && !this.CreateInterDBConn(dr[MLocationDEL.INTERCMPYDB].ToString(), dr[MLocationDEL.INTERCMPYDBPATH].ToString(), ref dbconInterComp))
                            {
                                strErrPrompt = Msg.Select("Can not connect intercompany database.", DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_CanNotConnectIntercompanyDB"));
                                return bValid;
                            }

                        }
                        return true;
                    }
                #endregion

                #region check InterCmpySupNo
                case "INTERCMPYSUPNO":
                    {
                        strValue = dr[MLocationDEL.INTERCMPYSUPNO].ToString();
                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.ISINTERCOMPANY].ToString()))
                        {
                            if (strValue.Trim() == "")
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_SupNoOnDBCanNotBeBlank");
                                return bValid;
                            }
                            bValid = DataFunctions.CheckRecordExist(dbcon, "Apsupplier", strValue, "SupNo");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_SupNoOnDBNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region check  InterCmpyCustNo
                case "INTERCMPYCUSTNO":
                    {
                        strValue = dr[MLocationDEL.INTERCMPYCUSTNO].ToString();
                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.ISINTERCOMPANY].ToString()))
                        {
                            if (strValue.Trim() == "")
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_CustNoOnDBCanNotBeBlank");
                                return bValid;
                            }
                            DBAccess dbconInterComp = null;

                            if (this.CreateInterDBConn(dr[MLocationDEL.INTERCMPYDB].ToString(), dr[MLocationDEL.INTERCMPYDBPATH].ToString(), ref dbconInterComp))
                            {

                                bValid = DataFunctions.CheckRecordExist(dbconInterComp, "ArCustomer", strValue, "CustNo");
                                if(dbconInterComp!=null) dbconInterComp.CloseCon();
                                if (!bValid)
                                {
                                    
                                    strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_CustNoOnDBNotOnFile");
                                    return bValid;
                                }
                                
                            }
                        }
                        return true;
                    }
                #endregion
                #region check  InterCmpyLocNo
                case "INTERCMPYLOCNO":
                    {
                        strValue = dr[MLocationDEL.INTERCMPYLOCNO].ToString();
                        if (BaseFunctions.GetBoolValue(dr[MLocationDEL.ISINTERCOMPANY].ToString()))
                        {
                            if (strValue.Trim() == "")
                            {
                                bValid = false;
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_LocNoOnDBCanNotBeBlank");
                                return bValid;
                            }
                            DBAccess dbconInterComp = null;

                            if (this.CreateInterDBConn(dr[MLocationDEL.INTERCMPYDB].ToString(), dr[MLocationDEL.INTERCMPYDBPATH].ToString(), ref dbconInterComp))
                            {

                                bValid = DataFunctions.CheckRecordExist(dbconInterComp, "mlocation", strValue, "LocationNo");
                                if(dbconInterComp!=null) dbconInterComp.CloseCon();
                                if (!bValid)
                                {
                                    strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_LocNoOnDBNotOnFile");
                                    return bValid;
                                }
                                
                            }
                        }
                        return true;
                    }
                #endregion
                    

                #region check physicalloc
                case "PHYSICALLOC":
                    {
                        strValue = dr[MLocationDEL.PHYSICALLOC].ToString();
                        if (strValue.Trim() != "")
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "mPhysicalLocation", strValue, "PhysicalLoc");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_PhysicalLocNotOnFile");
                                return bValid;
                            }
                        }
                        return true;
                    }
                #endregion

                #region about QuarantineLocation
                case "ISQUARANTINELOCATION":
                    if (DataFunctions.GetmSwitchValue(dbcon, "UseQuarantineLocation", "im"))
                    {

                    }
                    return true;
                    break;
                case "QUARANTINELOCATIONNO":
                    if (DataFunctions.GetmSwitchValue(dbcon, "UseQuarantineLocation", "im"))
                    {
                        if (strValue.Trim() != "")
                        {
                            bValid = DataFunctions.CheckRecordExist(dbcon, "mlocation", strValue, "locationno");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_QuarantineLocationNotOnFile");
                                return bValid;
                            }

                            bValid = DataFunctions.CheckRecordExist(dbcon, "mlocation", strValue, "locationno", "IsQuarantineLocation=1");
                            if (!bValid)
                            {
                                strErrPrompt = DataFunctions.GetMsg(dbcon, this.GetType().Name, "MSG_IsNotQuarantineLocation");
                                return bValid;
                            }
                        }
                    }
                    return true;
                    break;
                #endregion
                default:
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MLocationDEL.TABLENAME_MLOCATION, strFieldName, ref strErrPrompt);
            }
        }

        private bool CreateInterDBConn(string strDBname, string strServerName, ref DBAccess dBAccessInterComp)
        {
            string strConn = "";
            System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection()  ;
            try
            {
                strConn=DataFunctions.GetInterDBConnection(strDBname, strServerName);
                sqlConn.ConnectionString = strConn;
                sqlConn.Open();

                dBAccessInterComp = new DBAccess(false, false);
                dBAccessInterComp.Con = sqlConn;
                dBAccessInterComp.Com.Connection = sqlConn; 

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

		#region Insert into mLocation Valid
        public bool InsertValid(DBAccess dBAccess, MLocationDEL mLocationDEL)
		{
            bool bReturn = true;
            string strErrPrompt = "";

            DataRow dr;

            SysVar.ErrorMessage.Clear();
            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MLocationDEL.TABLENAME_MLOCATION + "'").DefaultView;

            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, mLocationDEL.Tables[MLocationDEL.TABLENAME_MLOCATION].Rows[0], drw["FieldName"].ToString(), ref  strErrPrompt, FormStatus.AddNew))
                {
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString().TrimEnd()));
                    }
                    bReturn = false;
                    break;
                }
            }

            return bReturn;
		}
		#endregion

		#region Update mLocation Valid
        public bool UpdateValid(DBAccess dBAccess, MLocationDEL mLocationDEL)
        {
            bool bReturn = true;
            string strErrPrompt = "";
            DataRow dr;

            SysVar.ErrorMessage.Clear();

            if (base.IsExistedAndNotChanged(dBAccess, mLocationDEL.Tables[MLocationDEL.TABLENAME_MLOCATION]))
            {
                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MLocationDEL.TABLENAME_MLOCATION + "'").DefaultView;

                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, mLocationDEL.Tables[MLocationDEL.TABLENAME_MLOCATION].Rows[0], drw["FieldName"].ToString(), ref  strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString().TrimEnd()));
                        }
                        break;
                    }
                }
            }
            else
            {
                bReturn = false;
            }
            if (Customize.UseCommissionCost())
            {
                string strCondition="LocationNo='"+mLocationDEL.Tables[MLocationDEL.TABLENAME_MLOCATION].Rows[0]["LocationNo"].ToString() +"'";
                string strFieldValueofSeller = mLocationDEL.Tables[MLocationDEL.TABLENAME_MLOCATION].Rows[0]["Seller"].ToString().TrimEnd() ;
                string strFieldValueofBuyer = mLocationDEL.Tables[MLocationDEL.TABLENAME_MLOCATION].Rows[0]["OwnBySlspNo"].ToString().TrimEnd() ;
                if (strFieldValueofSeller != "")
                {
                    if (!DataFunctions.CheckRecordExist("mlocationseller", strFieldValueofSeller, "slspno", strCondition))
                    {
                        SysVar.ErrorMessage.Add("Must enter the Commission% of the Default Seller.");
                        bReturn = false;
                    }
                    
                }
                if (strFieldValueofBuyer != "")
                {
                    if (!DataFunctions.CheckRecordExist("mlocationbuyer", strFieldValueofBuyer, "slspno", strCondition))
                    {
                        SysVar.ErrorMessage.Add("Must enter the Commission% of the Default Buyer.");
                        bReturn = false;
                    }
                   
                }
                
            }
            return bReturn;
        }

		#endregion

		#region Delete mLocation Valid
        public bool DeleteValid(DBAccess dBAccess, string strRecId, byte[] btRecStamp)
		{
            string strSql = "", strLocNo = "";

            SysVar.ErrorMessage.Clear();

            if (!base.IsExistedAndNotChanged(dBAccess, "mLocation", strRecId, btRecStamp))
            {
                return false;
            }

            strLocNo = DataFunctions.GetFieldValue(dBAccess, MLocationDEL.TABLENAME_MLOCATION, MLocationDEL.LOCATIONNO, "RecId=" + strRecId);

            strSql = "select top 1 recid from iminvloc where (Onhand<>0 or OnhandComm<>0 or OnOrder<>0 or Backorder<>0 or OnTransit<>0 or BmIPQty<>0 or BmQtyComm<>0 or Transferin<>0 or Transferout<>0) and LocationNo='" + strLocNo + "'";
            if (dBAccess.GetIntValue(strSql) > 0)
            {
                SysVar.ErrorMessage.Add(DataFunctions.GetMsg(dBAccess, this.GetType().Name, "MSG_LocHasBeenUsed"));
                return false;
            }

            strSql = "select top 1 recid from iminvhist where  LocationNo='" + strLocNo + "'";
            if (dBAccess.GetIntValue(strSql) > 0)
            {
                SysVar.ErrorMessage.Add(DataFunctions.GetMsg(dBAccess, this.GetType().Name, "MSG_LocHasBeenUsed"));
                return false;
            }

            return true;
		}

		#endregion
	}
}
